<?php
/**
 * Created by 1971599474@qq.com
 * User: zhixuan
 * Date: 2019/12/15
 * Time: 15:21
 */

namespace app\admin\model;

use app\common\enum\BaseEnum;
use app\common\utils\JsonService;
use app\common\utils\Random;

class Manage extends \app\common\model\Manage
{

    /*
     * 获取管理员
     */
    public static function manageList(){
        $params = array_filter(request()->param());
        $page = ! empty($params['page']) && $params['page'] >= 1 ? $params['page'] : 1;
        $where = [];
        if(!empty($params['id'])){
            $where[] = ['id','=',$params['id']];
        }
        if(!empty($params['full_name'])){
            $where[] = ['full_name','like','%'.$params['full_name'].'%'];
        }
        $condition = [
            'where' => $where,
            'order' => ['create_time' => 'desc'],
            'paginate' => [
                'page' => $page,
                'query' => $params,
            ],
        ];

        $manage = self::getPageAll($condition)->each(function($item){
            $item['status'] = BaseEnum::getBoolValue($item['status']);
        });
        return $manage;
    }

    /*
     * 新增管理员
     * */
    public static function createManage($post){
        try {
            $post['salt'] = Random::alnum();
            $post['entry_password'] = $post['password'];
            $post['password'] = md5(md5($post['password'].$post['salt']));
            self::create($post);
        } catch (\Exception $ex) {
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }

    /*
     * 更新管理员
     * */
    public static function updateManage($post){
        try {
            if (!empty($post['password'])) {
                $post['salt'] = Random::alnum();
                $post['entry_password'] = $post['password'];
                $post['password'] = md5(md5($post['password'] . $post['salt']));
            }else{
                unset($post['password']);
                unset($post['entry_password']);
            }
            self::updateOne($post, ['id' => $post['id']]);
        } catch (\Exception $ex) {
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }
}